﻿using System;
using System.Windows;
using JiaBao.Core.Data;
using JiaBao.Core.Infrastructure;
using JiaBao.Services.LogService;
using JiaBao.WPF.Framework;

namespace JiaBao.WPF.Setting
{
    /// <summary>
    /// W_ServerSetting.xaml 的交互逻辑
    /// </summary>
    public partial class W_ServerSetting :  IRegistrarWPFForm
    {
        DataSettings setting = EngineContext.Current.Resolve<DataSettings>();
 
        public W_ServerSetting()
        {
            InitializeComponent();
            try
            {
                txt_Server.Text = setting.Server;
                txt_DataBase.Text = setting.DataBase;
                txt_Uid.Text = setting.Uid;
                txt_Pwd.Text = setting.Pwd;
                AppStart.type = 0;
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
        }

        private void Btn_Save_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //保存设置
                var manager = new DataSettingsManager();
                setting.Server = txt_Server.Text.Trim();
                setting.DataBase = txt_DataBase.Text.Trim();
                setting.Uid = txt_Uid.Text.Trim();
                setting.Pwd = txt_Pwd.Text.Trim();
                setting.DataProvider = "sqlserver";
                setting.DataConnectionString =
                    String.Format("Data Source={0};Initial Catalog={1};Integrated Security=False;Persist Security Info=False;User ID={2};Password={3}",
                    setting.Server,
                    setting.DataBase,
                    setting.Uid,
                    setting.Pwd);
                manager.SaveSettings(setting);


                //重启
                AppStart.type = 2;
                MessageBox.Show("数据库设置完成 程序即将重启");

                Application.Current.Shutdown();
                System.Diagnostics.Process.Start(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Log.SaveLog(ex);
               
            }
        }

       
    }
}
